package com.gitee.zhangchenyan.takin.common.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import org.springframework.util.Base64Utils;

import java.nio.charset.StandardCharsets;

/**
 * @Deacription rsa
 * @Author zl
 * @Date 2021/12/5 13:32
 * @Version 1.0
 **/
public class RsaUtils {

    public static String signFormSHA256withRSA(String privateKey, String publicKey, String data) {
        Sign rsa = SecureUtil.sign(SignAlgorithm.SHA256withRSA, privateKey, publicKey);
        return Base64Utils.encodeToString(rsa.sign(data));
    }

    public static boolean verifyFromSHA256withRSA(String privateKey, String publicKey, String data, String sign) {
        Sign rsa = SecureUtil.sign(SignAlgorithm.SHA256withRSA, privateKey, publicKey);
        return rsa.verify(data.getBytes(StandardCharsets.UTF_8), Base64Utils.decodeFromString(sign));
    }
}
